package com.uenpay.im.ui.old.activity.red;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class GetSendOrReceivedRecordInvokItem extends HttpInvokeItem {

	public class GetSendOrReceivedRecordInvokItemResult extends
			HttpInvokeResult {
		public String myPacketsTotalAmount;
		public int myPacketsNum;
		public int myPacketsLuckNum;
		public ArrayList<RedEntity> arrayList = new ArrayList<RedEntity>();

	}

	public GetSendOrReceivedRecordInvokItemResult getOutput() {
		return (GetSendOrReceivedRecordInvokItemResult) GetResultObject();
	}

	public GetSendOrReceivedRecordInvokItem(int type, String year, int page) {
		// http://api.aitiapp.com/RedPackets/myPacketsList?year=&pageIndex=&pageSize=
		String url = "";
		if (type == 1) {
			url = APIConfiguration.getBaseUrl()
					+ "Purse/RedPackets/myPacketsList?year=" + year + "&pageIndex="
					+ page + "&pageSize=12&"
					+ APIConfiguration.getCustIdAndToken();
		} else {
			url = APIConfiguration.getBaseUrl()
					+ "Purse/RedPackets/mySendPacketsList?year=" + year
					+ "&pageIndex=" + page + "&pageSize=12&"
					+ APIConfiguration.getCustIdAndToken();
		}
		// http://api.aitiapp.com/RedPackets/mySendPacketsList?year=&pageIndex=&pageSize=

		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetSendOrReceivedRecordInvokItemResult result = new GetSendOrReceivedRecordInvokItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			JSONObject jsonObject = json.optJSONObject("data");
			if (jsonObject == null)
				return result;
			result.myPacketsLuckNum = jsonObject.optInt("myPacketsLuckNum");
			result.myPacketsNum = jsonObject.optInt("myPacketsNum");
			result.myPacketsTotalAmount = jsonObject
					.optString("myPacketsTotalAmount");
			JSONArray arr = jsonObject.optJSONArray("myPacketsList");
			if (arr != null && arr.length() > 0) {
				for (int i = 0, length = arr.length(); i < length; i++) {
					result.arrayList.add(desRedEntity(arr.optJSONObject(i)));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}
		return result;
	}

	private RedEntity desRedEntity(JSONObject json) {
		RedEntity entity = new RedEntity();
		entity.id = json.optInt("id");
		if (json.has("number")) {
			entity.number = json.optInt("number");
		}
		if (json.has("opennumber")) {
			entity.opennumber = json.optInt("opennumber");
		}
		entity.amount = json.optString("amount");
		entity.packetsid = json.optString("packetsid");
		entity.custid = json.optString("custid");
		if (json.has("fnickname")) {
			entity.fnickname = json.optString("fnickname");
		}
		entity.createtime = json.optString("createtime");
		if (json.has("fcustid")) {
			entity.fcustid = json.optString("fcustid");
		}
		if (json.has("isluck")) {
			entity.isluck = json.optInt("isluck");
		}
		entity.type = json.optInt("type");
		return entity;
	}

}
